package com.unittec.zk.provider.exam.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.unittec.zk.provider.utils.UUIDUtils;
import com.unittec.zk.sdk.exam.request.*;
import com.unittec.zk.sdk.root.internal.exception.BusinessException;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unittec.zk.provider.exam.mapper.ElRepoMapper;
import com.unittec.zk.provider.exam.entity.ElRepo;

@Service
public class ElRepoService extends ServiceImpl<ElRepoMapper, ElRepo> {

    /**
     * 题库管理分页
     *
     * @param req201001
     * @return
     */
    public Object getRepoPage(Req201001 req201001) {
        //加载一个条件构造器
        LambdaQueryWrapper<ElRepo> qw = new LambdaQueryWrapper<>();

        //如果‘题目’参数不为空，则将其添加到条件构造器中
        if (StrUtil.isNotBlank(req201001.getTitle())) {
            qw.like(ElRepo::getTitle, req201001.getTitle());
        }

        //根据条件构造器和分页参数，去查询数据库
        Page<Map<String, Object>> page = this.baseMapper.getElRePoPage(new Page<Object>(req201001.getCurrent(), req201001.getSize()), qw);

        //返回结果
        return page;
    }

    /**
     * 题库-增
     *
     * @param req201002
     * @return
     */
    public Object addRepo(Req201002 req201002) {
        ElRepo elRepo = new ElRepo();

        //设置ID
        elRepo.setId(UUIDUtils.getId());

        //如果题目不为空，为题库设置题目;若为空，则返回错误信息
        if (StrUtil.isNotBlank(req201002.getTitle())) {
            elRepo.setTitle(req201002.getTitle());
        }else{
            throw new BusinessException("请输入题目");
        }

        //为题库设置备注
        elRepo.setRemark(req201002.getRemark());

        //增加一条记录
        this.save(elRepo);

        //返回结果
        return "Success";
    }

    /**
     * 题库-改
     * @param req201003
     * @return
     */
    public Object updateRepo(Req201003 req201003) {
        //初始化实体类
        ElRepo elRepo = new ElRepo();

        //设置参数
        elRepo.setId(req201003.getId());
        elRepo.setTitle(req201003.getTitle());
        elRepo.setRemark(req201003.getRemark());

        //根据id修改记录
        this.updateById(elRepo);

        //返回结果
        return "Success";
    }

    /**
     * 题库-查
     * @param req201004
     * @return
     */
    public Object getRepoById(Req201004 req201004) {
        //根据id查询记录
        ElRepo elRepo = this.getById(req201004.getId());

        //返回结果
        return elRepo;
    }

    /**
     * 题库-删
     * @param req201005
     * @return
     */
    public Object delRepo(Req201005 req201005) {
        //根据id删除对应记录
        this.removeByIds(req201005.getIds());

        //返回结果
        return "Success";
    }

    /**
     * 题库-列表
     * @param req201007
     * @return
     */
    public Object getRepoList(Req201007 req201007) {

        //返回题库列表
        return this.list();
    }

    public void refreshStat(String repoId) {
        baseMapper.refreshStat(repoId);
    }
}

